package com.sgb.kjwl.viewmodel.material;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.model.entity.SearchMaterialEntity3;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson3;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.activity.material.MaterialListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 工程材料列表（1 筛选条件请求 2 列表数据请求）
 */
public class MaterialListModel extends BaseViewModel {
    public ObservableField<MaterialFilterJson3> materialFilterJson = new ObservableField<>(new MaterialFilterJson3());
    public ObservableField<String> areaCode = new ObservableField<>("");
    private MaterialListActivity activity;
    private MaterialRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public MaterialListModel(MaterialListActivity activity, MaterialRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
        areaCode.set(repertory.getAreaCode());
    }

    public void getMaterialModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        materialFilterJson.get().setPage(page);
        materialFilterJson.get().setSize(size);
        materialFilterJson.get().setAreaCode(areaCode.get());
        if (materialFilterJson.get().getPriceSort() == 0) {
            materialFilterJson.get().setTimeSort(2);
        } else {
            materialFilterJson.get().setTimeSort(0);
        }
        repertory.getMaterialList3(materialFilterJson.get()).subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMaterialEntity3>>>>() {
            @Override
            public void accept(BaseEntity<ListResponse<List<SearchMaterialEntity3>>> listResponseBaseEntity) throws Exception {
                if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
            }
        });
    }

    public void getAllArealModel() {
        APIManager.getInstance().getCommonAPI().getCurrentArea().enqueue(new Callback<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> call, Response<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> response) {
                if (response.body().getData() == null)
                    return;
                activity.showAreaPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ProvinceEntity<List<ProvinceEntity>>>>> call, Throwable t) {

            }
        });
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    public int getSelectModel(String currentId, List<ProvinceEntity<List<ProvinceEntity>>> datas) {
        for (int i = 0; i < datas.size(); i++) {
            if (String.valueOf(datas.get(i).getId()).equals(currentId)) {
                return i;
            }
        }
        return 0;
    }

    public void getFilterTypeModel(String id) {
        repertory.getMaterialFilterType(id, areaCode.get())
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showTypePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getFilterNameModel(String id) {
        repertory.getMaterialFilterName(id, areaCode.get())
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showNamePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getFilterStoreModel() {
        repertory.getMaterialFilterStore(areaCode.get())
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showStorePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getFilterBrandModel(String id) {
        repertory.getMaterialFilterBrand(id, areaCode.get())
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showBrandPopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getFilterBrandSpec(String id) {
        repertory.getMaterialFilterSpec(id, areaCode.get())
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showFilterBrandSpec(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void getFilterDistance() {
        repertory.getMaterialDistanceData()
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showDistancePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }


    public void checkToken(String tag) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().checkToken().enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.checkTokenView();
                } else {
                    if (!"Miss_Permission".equals(response.body().getCode())) {
                        repertory.saveToken(null);
                        activity.toLogin();
                    }
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
